Estoy un poco confundido con esta advertencia:
Argument type number is not assignable to parameter type string | undefined Type number is not assignable to type string
Tome la siguiente paz de código:
function createCalculator() { let calculator = { sum() { return this.a + this.b; }, mul() { return this.a * this.b; }, read() { this.a = +prompt('a?', 0); this.b = +prompt('b?', 0); } }; calculator.read([1,3,6]); console.log( calculator.sum() ); console.log( calculator.mul() ); } let calculator; calculator = createCalculator();También tengo una advertencia:
Void function return value is usedquiero lo siguiente:
La función createCalculator() devuelve un objeto con tres métodos:
read(arr) acepta una tabla de números y la guarda en su objeto de campo.sum() devuelve la suma de los valores de la tablamul() devuelve el producto de los valores de la tablaHe insertado una función de retorno que debería hacer desaparecer su advertencia.
function createCalculator() { let calculator = { sum() { return this.a + this.b; }, mul() { return this.a * this.b; }, read() { this.a = +prompt('a?', 0); this.b = +prompt('b?', 0); } }; calculator.read(); // Don't bother to send in the [1,3,6] because you are not using it in the function. console.log(calculator.sum()); console.log(calculator.mul()); return calculator // Do this so that your final statement really has something to receive! } let calculator; calculator = createCalculator(); function createCalculator() { return { sum() { return this.a + this.b; }, mul() { return this.a * this.b; }, read() { this.a = +prompt('a?', 0); this.b = +prompt('b?', 0); } }; } const calculator = createCalculator(); calculator.read(); console.log(calculator.sum()); console.log(calculator.mul());Entonces, ¿qué tal esto?
function createCalculator() { return { memory: [], read(arr) { this.memory = arr }, sum() { return this.memory.reduce((x, y) => x + y, 0) }, mul() { return this.memory.reduce((x, y) => x * y, 1) }, }; } const calculator = createCalculator(); calculator.read([1, 3, 6]); console.log(calculator.sum()); console.log(calculator.mul());